package com.loong.musicdemo.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.loong.musicdemo.R;
import com.loong.musicdemo.UserInfoActivity;
import com.loong.musicdemo.model.User;

import java.io.IOException;
import java.util.List;

import com.loong.musicdemo.util.httpUtil.HttpUtil;
import com.loong.musicdemo.widget.CustomDialog.CommonDialog;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

public class FriendsAdapter extends RecyclerView.Adapter<FriendsAdapter.ViewHolder> {
    private final List<User> friends;
    private final int type;//类型0:粉丝 1:关注

    public FriendsAdapter(List<User> friends, int i) {
        this.friends = friends;
        this.type = i;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_my_friend, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        User friend = friends.get(position);
        holder.nickName.setText(friend.getNickName());
        if (friend.getSignature() == null || friend.getSignature().equals("") || friend.getSignature().equals("null")) {
            holder.signature.setText("");
        } else {
            holder.signature.setText(friend.getSignature());
        }
        int genderSrc = ((friend.getGender() == 0) ? R.drawable.gender_nv : R.drawable.gender_nan);
        Glide.with(holder.friend).load(genderSrc).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.gender);
        Glide.with(holder.friend).load(friend.getAvatarUrl()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.titleImg);
        holder.friend.setOnClickListener(v -> {
            Intent intent = new Intent(v.getContext(), UserInfoActivity.class);
            intent.putExtra("userid", friend.getUserId());//传入用户id用于获取详细信息
            v.getContext().startActivity(intent);//打开用户信息
        });
        holder.friendsSetting.setOnClickListener(v -> {//当type 为1 关注用户 type为0 取关用户
            String title = type == 1 ? "关注" : "取消关注";
            CommonDialog dialog = new CommonDialog(v.getContext());
            dialog.setTitle(title)
                    .setMessage("确定要" + title + "该用户?")
                    .setPositive("确认")
                    .setNegative("取消")
                    .setOnclickBottomListener(new CommonDialog.OnclickBottomListener() {
                        @Override
                        public void onPositiveClick() {
                            HttpUtil.sendOKHttpRequest("http://47.112.224.146:3000/follow?id=" + friend.getUserId() + "&t=" + type, new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) {
                                }
                            });
                            //确认操作
                            Toast.makeText(v.getContext(), title + friend.getNickName() + "成功!", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }

                        @Override
                        public void onNegativeClick() {
                            //取消操作
                            dialog.dismiss();
                        }
                    }).show();
        });
    }

    @Override
    public int getItemCount() {
        return friends.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout friend;//点击用的容器
        CircleImageView titleImg;//头像
        TextView nickName;//昵称展示
        ImageView gender;//性别
        TextView signature;//个签
        ImageView friendsSetting;//设置

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            friend = itemView.findViewById(R.id.friend);
            titleImg = itemView.findViewById(R.id.title_img);
            nickName = itemView.findViewById(R.id.nickname);
            gender = itemView.findViewById(R.id.gender);
            signature = itemView.findViewById(R.id.signature);
            friendsSetting = itemView.findViewById(R.id.friends_setting);
        }
    }
}
